import { Application } from 'egg';

export default (app: Application) => {
  const { controller, router } = app;
  app.beforeStart(async () => {
    await app.model.sync({ force: false });//force  false 为不覆盖 true会删除再创建; alter true可以 添加或删除字段;
  });
  // app.middleware.checkRouter();
  /*设置egg-session-redis */
  app.sessionStore = {
    async get(key) {
      const res = await app.redis.get(key);
      if (!res) return null; 
      return JSON.parse(res);
    },
    async set(key, value, maxAge) {
      if (!maxAge) maxAge = 24 * 60 * 60 * 1000;
      value = JSON.stringify(value);
      await app.redis.set(key, value, 'PX', maxAge);
      // await redis.set(key, value, 'EX', seconds);
    },
    async destroy(key: any) {
      await app.redis.del(key);
    },
  };

  // const jwt = app.middleware.jwt(app.config.jwt);
  // router.get('/', jwt, controller.user.register);
  // router.get('/', controller.user.register);
  require('./router/user')(app);
  require('./router/goods')(app);
  require('./router/collection')(app);
  require('./router/follow')(app);
  router.post('/api/v1/repot_goods', controller.report.repotGoods);
  router.post('/api/v1/submit_feedback', controller.feedback.submitFeedback);
  router.get('/api/v1/get_notice', controller.otherOperations.getNotice);
  router.post('/api/v1/set_swiper', controller.swiper.addSwiper);
  router.get('/api/v1/get_swiper', controller.swiper.getSwiper);

};
